package num13;

/**
 * 13. 罗马数字转整数
 * 自己的解法
 */
class Solution {

    int[] val = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
    String[] str = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};

    public int romanToInt(String s) {
        int r = 0;
        for (int i = 0; i < str.length; i++) {
            int cv = val[i];
            String cs = str[i];

            while (s.startsWith(cs)) {
                r += cv;
                s = s.equals(cs) ? "" : s.substring(cs.length());
            }
        }
        return r;
    }

    public static void main(String[] args) {
        System.out.println(new Solution().romanToInt("MCMXCIV"));
    }
}